﻿(function (module) {
    module.filter('gender', function () {

        return function (input) {

            switch (input) {
                case 0:
                    return "未知";
                case 1:
                    return "男";
                case 2:
                    return "女";
                default:
                    return "";
            }
        };
    });
    module.filter('trustHtml', ['$sce', function ($sce) {
        return function (html) {
            return $sce.trustAsHtml(html);
        };
    }]);
    module.filter('cut', function () {
        return function (value, wordwise, max, tail) {
            if (!value) return '';

            max = parseInt(max, 10);
            if (!max) return value;
            if (value.length <= max) return value;

            value = value.substr(0, max);
            if (wordwise) {
                var lastspace = value.lastIndexOf(' ');
                if (lastspace != -1) {
                    value = value.substr(0, lastspace);
                }
            }

            return value + (tail || ' …');
        };
    });
    module.filter('trustUrl', ['$sce', function ($sce) {
        return function (url) {
            return $sce.trustAsResourceUrl(url);
        };
    }]);
    module.filter('avator', function () {
        return function (input) {
            if (!input || input.length === 0) {
                return "images/demo/photo_default_user.jpg";
            }
            return input;
        };
    });
    module.filter('qrcode', function () {
        return function (input) {
            if (!input || input.length === 0) {
                return "images/demo/code_ali.jpg";
            }
            return input;
        };
    });
    module.filter('storeavator', function () {
        return function (input) {
            
            if (!input || input.length === 0) {
                return "images/demo/store_default.png";
            }
            return input;
        };
    });
    module.filter('statusstr', function () {

        return function (input) {

            switch (input) {
                case 0:
                    return "待审核";
                case 1:
                    return "审核通过";
                case 2:
                    return "审核未通过";
                default:
                    return "";
            }
        };
    });
    module.filter('getage', function () {
        return function (birthday) {
            var age = -1;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth() + 1;
            var todayDay = today.getDate();
            var birthday = new Date(birthday);
            if (birthday) {
                birthdayYear = birthday.getFullYear();
                birthdayMonth = birthday.getMonth();
                birthdayDay = birthday.getDate();
                if (todayYear - birthdayYear < 0) {
                    alert("出生日期选择错误!");
                }
                else {
                    if (todayMonth * 1 - birthdayMonth * 1 < 0) {
                        age = (todayYear * 1 - birthdayYear * 1) - 1;
                    }
                    else {
                        if (todayDay - birthdayDay >= 0) { 
                            age = (todayYear * 1 - birthdayYear * 1);
                        }
                        else {
                            age = (todayYear * 1 - birthdayYear * 1) - 1;
                        }
                    }
                }
                return age * 1;
            }
            else {
                return -1;
            }
        };
    });
 
})(angular.module('app.filters'));